package com.wzy.server.controller;


import com.wzy.server.pojo.Admin;
import com.wzy.server.pojo.ChatMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import java.time.LocalDateTime;

//websocket

@Controller
public class WsController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping("/ws/chat")
    public void handleMsg(Authentication authentication, ChatMsg chatMsg){
        Admin admin=(Admin)authentication.getPrincipal();//获取当前对象
        chatMsg.setFrom(admin.getUsername());//登录用户名
        chatMsg.setFromNickName(admin.getName());//显示的用户名昵称
        chatMsg.setDate(LocalDateTime.now());//时间
//                              发送消息              发送给谁                  队列           消息
        simpMessagingTemplate.convertAndSendToUser(chatMsg.getTo(),"queue/chat",chatMsg);

    }







}
